import org.junit.Test;

public class MyTest {
    @Test
    public void test1(){
        int[] arr={5,4,3,2,1}; 
        heapSorting(arr);
        for(int i:arr){
            System.out.print(i+" ");
        }
    }

    private void heapSorting(int[] arr){
        for(int i=(arr.length>>1)-1;i>=0;i--){
            heapAdjusting(arr, i, arr.length);
        }
        for(int i=arr.length-1;i>0;i--){
            int temp=arr[0];
            arr[0]=arr[i];
            arr[i]=temp;
            heapAdjusting(arr, 0, i);
        }
    }
    private void heapAdjusting(int[] arr,int i,int length){
        for(int k=(i<<1)+1;k<length;k=(k<<1)+1){
            int temp=arr[i];
            if(k+1<length&&arr[k]<arr[k+1]){
                k++;
            }
            if(arr[k]>temp){
                arr[i]=arr[k];
                arr[k]=temp;
            }
            i=k;
        }
    }
}
